package view.CURD;
import library.controller.FrameListener;
import library.controller.JTextFieldListener;
import library.controller.LoginListener;
import library.po.User;
import view.login.LoginDialog;
import view.main.MainFrame;

import javax.swing.*;

import java.awt.Dimension;
import java.awt.Font;

/**
 * 添加用户信息对话框
 * @author 27261
 *
 */
public class UserDialog extends JDialog {
    private static final long serialVersionUID = 1L;
    private LoginListener loginListener;
    private FrameListener frameListener;
    private User user;
    private JTextField userIdTextField = new JTextField(20);
    private JTextField userNameTextField = new JTextField(20);
    private JPasswordField passwordField = new JPasswordField(20);
    private JLabel idLabel = new JLabel("用户名:");
    private JLabel passwordLabel = new JLabel("密 码");
    private JLabel nameLabel = new JLabel("姓 名:");
    private JLabel sexLabel = new JLabel("性 别:");
    private JLabel typeLabel = new JLabel("身 份:");
    private JComboBox sexBox = new JComboBox();
    private JComboBox typeBox = new JComboBox();
    private JButton okButton = new JButton("确认");
    private JButton cancelButton = new JButton("重置");
    private JPanel buttonBar = new JPanel();
    private JPanel userPanel = new JPanel();
    public UserDialog(LoginDialog owner, String title, User user) {
        this.loginListener = owner.getLoginListener();  //让UserFrame和Dialog共用同一个controller对象
        this.loginListener.setUserAddDialog(this);
        this.setTitle(title);
        this.setResizable(false);
        this.user = user;
        this.add(getUserPanel());
        this.setModal(true);
        this.setSize(250, 320);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.closeCurrentDialog();
    }
    public UserDialog(MainFrame owner, String title, User user) {
        this.frameListener = owner.getMainListener();  //让MainFrame和Dialog共用同一个controller对象
        this.loginListener.setUserAddDialog(this);
        this.setTitle(title);
        this.setResizable(false);
        this.user = user;
        this.add(getUserPanel());
        this.setModal(true);
        this.setSize(250, 320);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.closeCurrentDialog();
    }
    public void closeCurrentDialog() {
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }
    /**
     * 添加学生信息表单面板
     * @return
     */
    private JPanel getUserPanel() {
        userPanel = new JPanel();
        if("注册".equals(this.getTitle())||"添加用户信息".equals(this.getTitle())) {
            userIdTextField.addFocusListener(new JTextFieldListener(userIdTextField, "用户名由20位以下字母和数字组成"));
            passwordField.addFocusListener(new JTextFieldListener(passwordField, "密码由20位以下字母和数字组成"));
            userNameTextField.addFocusListener(new JTextFieldListener(userNameTextField, "您的姓名"));
            okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
            okButton.setPreferredSize(new Dimension(100, 30));
            okButton.setActionCommand("userAddPost"); //添加学生信息
            sexBox.addItem("--请选择--");
            sexBox.addItem("男");
            sexBox.addItem("女");
            typeBox.addItem("--请选择--");
            typeBox.addItem("借阅者");
            typeBox.addItem("系统管理员&&借阅者");
            typeBox.addItem("图书管理员&&借阅者");
            typeBox.addItem("系统管理员&&图书管理员&&借阅者");
        }else if ("用户修改信息".equals(this.getTitle())){
            sexBox.addItem(this.user.getUserSex());
            typeBox.addItem(this.loginListener.typeToIdentity(user.getUserType()));
            typeBox.setEditable(false);
            okButton = new JButton("确定修改");
            okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
            okButton.setPreferredSize(new Dimension(100, 30));
            okButton.setActionCommand("userUpdatePost"); //修改学生信息
        }
        okButton.addActionListener(loginListener);
        cancelButton.addActionListener(loginListener);
        idLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        passwordLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        nameLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        sexLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        typeLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        userPanel.add(idLabel);
        userPanel.add(userIdTextField);
        userPanel.add(passwordLabel);
        userPanel.add(passwordField);
        userPanel.add(nameLabel);
        userPanel.add(userNameTextField);
        userPanel.add(sexLabel);
        userPanel.add(sexBox);
        userPanel.add(typeLabel);
        userPanel.add(typeBox);
        userPanel.add(okButton);
        userPanel.add(cancelButton);
        return userPanel;
    }
    public UserDialog() {
        super();
    }
    public JTextField getUserIdTextField(){
        return this.userIdTextField;
    }
    public JTextField getUserNameTextField(){
        return this.userNameTextField;
    }
    public JPasswordField getPasswordField(){
        return this.passwordField;
    }
    public JComboBox getSexBox(){
        return this.sexBox;
    }
    public JComboBox getTypeBox(){
        return this.typeBox;
    }

}
